<#macro skeleton>

<!doctype html>
<html>
<head>
    <script type="application/javascript">
        var _context = '<@spring.url "/"/>';
        var _msgVersion = '${msgRevision}';
    </script>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta lang=""/>

    <@app.cssLink 'bootstrap.css'/>
    <@app.cssLink 'jquery.pnotify.default.css'/>
    <@app.cssLink 'style.css'/>

    <@app.jsLink 'jquery-1.9.1.js'/>
    <@app.jsLink 'jquery.cookie.js'/>
    <@app.jsLink 'jquery.i18n.properties-1.0.9.patched.js'/>
    <@app.jsLink 'jquery.pnotify.js'/>
    <@app.jsLink 'bootstrap.js'/>
    <@app.jsLink 'commons.js'/>
    <@app.jsLink 'i18n.js'/>

    <!--[if IE]>
        <@app.jsLink 'noie.js'/>
    <![endif]-->

    <title>MergeInformer</title>

    <script type="application/javascript">
        var _locale;
        <#assign isAuthenticated=false/>
        <@security.authorize access="isAuthenticated()">
            <#assign isAuthenticated=true/>
        </@security.authorize>
        <#if isAuthenticated>
            _locale = '<@security.authentication property="principal.language.locale"/>';
        <#else>
            _locale = $.cookie('mergeinformer.locale.key');
        </#if>
    </script>

    <#-- notifications -->
    <script type="application/javascript">
        $(document).ready(function () {
            <#if notifications?? && (notifications?size > 0)>
                <#list notifications as notification>
                    popNotification({
                        type: '${notification.type}',
                        title: '${notification.title}',
                        text: '${notification.text}'
                    });
                </#list>
            </#if>
        });
    </script>
</head>
<body>
<div class="container">
    <ul class="breadcrumb navbar-fixed-top" role="navigation">
        <div class="pull-right">
            <form id="author-filter-form" class="form-search" method="get">
                <div class="input-append">
                    <#assign filterValue = ''/>
                    <#if (authorsFilter?size > 0)>
                        <#assign filterValue = authorsFilter[0]/>
                    </#if>
                    <#if (authorsFilter?size > 1)>
                        <#list authorsFilter[1..authorsFilter?size - 1] as authorFilter>
                            <#assign filterValue = filterValue + '|' + authorFilter/>
                        </#list>
                    </#if>
                    <input id="author" name="author" type="text" autocomplete="off" value="${filterValue}" placeholder="Filter by author" class="search-query input-medium"/>
                    <button id="author-reset-button" type="button" class="btn">reset</button>
                </div>
            </form>
        </div>

        <script type="application/javascript">
            var authorsFilterForm = $('#author-filter-form');
            var author = $('#author');
            authorsFilterForm.submit(function () {
                author.val($.trim(author.val()));
            });
            $(author).typeahead({
                source: <@toJsArray Request.authors/>,
                updater: function (item) {
                    author.val(item);
                    authorsFilterForm.submit();
                    return item;
                }
            });
            $('#author-reset-button').click(function () {
                author.val('');
                authorsFilterForm.submit();
            });
        </script>

        <div class="pull-right">
            <form id="merged-filter-form" class="form-horizontal" method="get">
                <label for="merged-filter" class="control-label">Show merged</label>
                <div class="controls">
                    <div class="control-group">
                        <select id="merged-filter" name="merged" class="input-medium">
                            <#assign mergedFilterOptions = {
                                "2": "Projects & tasks",
                                "1": "Projects",
                                "0": "None"}/>
                            <#list mergedFilterOptions?keys as key>
                                <option value="${key}"
                                    <#if key?number == mergedFilter?number>
                                        selected="selected"
                                    </#if>
                                        >${mergedFilterOptions[key]}</option>
                            </#list>
                        </select>
                    </div>
                </div>
            </form>
        </div>

        <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown" href="javascript:">
                <#if currentProject??>
                    ${currentProject.name}
                <#else>
                    All projects
                </#if>
                <b class="caret"></b>
            </a>
            <ul class="dropdown-menu" role="menu">
                <li>
                    <a href="<@spring.url '/'/>">
                        <div class="project ${defineAllProjectsClass(projects)}">
                            &nbsp;
                        </div>
                        <span
                            <#if !currentProject??>
                                class="underlined"
                            </#if>
                                >
                            All projects
                        </span>
                    </a>
                </li>

                <#if (projects?size > 0)>
                    <li class="divider"></li>
                    <#list projects as project>
                        <li class="dropdown-submenu">
                            <@projectMenuEntry project currentProject/>
                            <ul class="dropdown-menu" role="menu">
                                <@listTasks project/>

                                <li class="divider"></li>
                                <#assign url = ''>
                                <#if (project.tasks?size > 0)>
                                    <#assign url = '/${project.shortName}?execute'>
                                </#if>
                                <@menuEntry 'Update all tasks' url 'icon-refresh'/>
                                <@menuEntry 'New task' '/${project.shortName}?new-task' 'icon-plus'/>

                                <li class="divider"></li>
                                <@menuEntry 'Edit project' '/${project.shortName}?edit' 'icon-edit'/>
                                <@deleteProjectMenuEntry project/>
                            </ul>
                        </li>
                    </#list>
                </#if>

                <li class="divider"></li>
                <li
                    <#if (projects?size == 0)>
                        class="disabled"
                    </#if>
                        >
                    <a id="update-all-projects" href="<@spring.url '/?execute'/>">
                        <div>
                            <i class="icon-refresh"></i>
                        </div>
                        Update all projects
                    </a>
                </li>
                <@menuEntry 'New project' '?new-project' 'icon-plus'/>
            </ul>
            <#if currentProject??>
                <span class="divider">></span>
            </#if>
        </li>
        <#if currentProject??>
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="javascript:">
                    <#if currentTask??>
                        ${currentTask.name}
                    <#else>
                        All tasks
                    </#if>
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu" role="menu">
                    <#if (currentProject.tasks?size > 0)>
                        <li>
                            <a href="<@spring.url '/${currentProject.shortName}'/>">
                                <div class="task ${defineAllTasksClass(currentProject.tasks)}">
                                    &nbsp;
                                </div>
                                <span
                                    <#if !currentTask??>
                                        class="underlined"
                                    </#if>
                                        >
                                    All tasks
                                </span>
                            </a>
                        </li>

                        <li class="divider"></li>
                        <@listTasks currentProject/>
                    <#else>
                        <@menuEntry 'No tasks' '' ''/>
                    </#if>

                    <li class="divider"></li>
                    <@menuEntry 'Update all tasks' '/${currentProject.shortName}?execute' 'icon-refresh'/>
                    <@menuEntry 'New task' '/${currentProject.shortName}?new-task' 'icon-plus'/>
                </ul>
            </li>
        </#if>
    </ul>
    <!-- content -->
    <#nested/>
    <!-- / content -->
</div>
<footer class="text-center">
    ${buildInfo}
</footer>
</body>
</html>

<#--Modals-->

<#-- Delete project confirmation modal -->
<div id="delete-project-modal" class="modal hide" data-backdrop="static">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Delete project</a></h3>
    </div>
    <div class="modal-body">
        <p>Do you really want to delete project <b class="project-b"></b> and all it's tasks?</p>
    </div>
    <div class="modal-footer">
        <img class="modal-indicator pull-left hide" src="<@app.imgUrl 'ajax-loader.gif'/>">
        <a href="" class="btn btn-primary">Yes</a>
        <button class="btn" data-dismiss="modal">Cancel</button>
    </div>
</div>

<#-- Delete task confirmation modal -->
<div id="delete-task-modal" class="modal hide" data-backdrop="static">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Delete task</a></h3>
    </div>
    <div class="modal-body">
        <p>Do you really want to delete task <b class="task-b"></b> of project <b class="project-b"></b>?</p>
    </div>
    <div class="modal-footer">
        <img class="modal-indicator pull-left hide" src="<@app.imgUrl 'ajax-loader.gif'/>">
        <a href="" class="btn btn-primary">Yes</a>
        <button class="btn" data-dismiss="modal">Cancel</button>
    </div>
</div>

<#-- Update all projects confirmation modal -->
<div id="update-all-projects-modal" class="modal hide" data-backdrop="static">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Update all projects</a></h3>
    </div>
    <div class="modal-body">
        <p>Do you really want to update all projects?</p>
        <p>This operation can take some time to complete</p>
    </div>
    <div class="modal-footer">
        <img class="modal-indicator pull-left hide" src="<@app.imgUrl 'ajax-loader.gif'/>">
        <a href="<@spring.url '/?execute'/>" class="btn btn-primary">Update</a>
        <button class="btn" data-dismiss="modal">Cancel</button>
    </div>
</div>

</#macro>


<#-- macro definitions ------------------------------------------------------------------------------------------------>

<#macro deleteProjectMenuEntry project>
    <li>
        <a class="delete-project-a" project-name="${project.name}" href="<@spring.url '/${project.shortName}?delete'/>">
            <div>
                <i class="icon-remove"></i>
            </div>
            Delete project
        </a>
    </li>
</#macro>

<#macro deleteTaskMenuEntry project task>
<li>
    <a class="delete-task-a" project-name="${project.name}" task-name="${task.name}" href="<@spring.url '/${project.shortName}/${task.shortName}?delete'/>">
        <div>
            <i class="icon-remove"></i>
        </div>
        Delete task
    </a>
</li>
</#macro>

<#macro listTasks project>
    <#if project.tasks?size == 0>
        <@menuEntry 'No tasks' '' ''/>
    <#else>
        <#list project.tasks as task>
            <#assign selected = ''/>
            <#if currentTask?? && currentTask == task>
                <#assign selected = 'true'/>
            </#if>
            <li class="dropdown-submenu task ${defineTaskClass(task)}" role="menu">
                <@taskMenuEntry project task selected/>
                <ul class="dropdown-menu" role="menu">
                    <@menuEntry 'Update now' '/${project.shortName}/${task.shortName}?execute' 'icon-refresh'/>

                    <li class="divider"></li>
                    <@menuEntry 'Edit task' '/${project.shortName}/${task.shortName}?edit' 'icon-edit'/>
                    <@deleteTaskMenuEntry project task/>
                </ul>
            </li>
        </#list>
    </#if>
</#macro>

<#macro menuEntry name url='' icon=''>
    <li
        <#if !url?has_content>
            class="disabled"
        </#if>
        >
        <a
            <#if url?has_content>
                    href="<@spring.url '${url}'/>"
            <#else>
                    href="javascript:" class="disabled"
            </#if>
                >
            <div>
                <#if icon?has_content>
                    <i class="${icon}"></i>
                <#else>
                    &nbsp;
                </#if>
            </div>
        ${name}
        </a>
    </li>
</#macro>

<#macro projectMenuEntry project currentProject=''>
<a href="<@spring.url '/${project.shortName}'/>">
    <div class="project ${defineProjectClass(project)}">
        &nbsp;
    </div>
    <span
        <#if currentProject == project>
            class="underlined"
        </#if>
            >
        ${project.name}
    </span>
</a>
</#macro>

<#macro taskMenuEntry project task selected=''>
<a href="<@spring.url '/${project.shortName}/${task.shortName}'/>">
    <div class="task ${defineTaskClass(task)}">
        &nbsp;
    </div>
    <span <#if selected?has_content>
            class="underlined"
    </#if>
            >
        ${task.name}
    </span>
</a>
</#macro>

<#macro entry name url='' icon=''>
<a
    <#if url?has_content>
            href="<@spring.url '${url}'/>"
    <#else>
            href="javascript:" class="disabled"
    </#if>
        >
        <div>
            <#if icon?has_content>
                <i class="${icon}"></i>
            <#else>
                &nbsp;
            </#if>
        </div>
        ${name}
</a>
</#macro>

<#macro toJsArray array>
[
    <#if (array?size > 0)>
    "${array[0]}"
        <#if (array?size > 2)>
            <#list array[1..array?size - 1] as element>
            ,"${element}"
            </#list>
        </#if>
    </#if>
]
</#macro>


<#-- functions -------------------------------------------------------------------------------------------------------->

<#function defineAllProjectsClass projects>
    <#assign class = 'empty'/>
    <#if (projects?size > 0)>
        <#list projects as project>
            <#assign projectClass = defineProjectClass(project)/>
            <#switch projectClass>
                <#case 'failed'>
                    <#return projectClass/>
                <#case 'unmerged'>
                    <#assign class = projectClass/>
                    <#break/>
                <#case 'merged'>
                    <#if class != 'unmerged'>
                        <#assign class = projectClass/>
                    </#if>
                    <#break/>
                <#default> <#-- 'empty' falls here -->
            </#switch>
        </#list>
    </#if>
    <#return class/>
</#function>

<#function defineAllTasksClass tasks>
    <#assign class = 'empty'/>
    <#if (tasks?size > 0)>
        <#assign haveExecuted = false/>
        <#list tasks as task>
            <#if task.executed??>
                <#assign haveExecuted = true/>
                <#break/>
            </#if>
        </#list>
        <#if haveExecuted>
            <#assign class = 'merged'/>
            <#list tasks as task>
                <#assign taskClass = defineTaskClass(task)/>
                <#switch taskClass>
                    <#case 'failed'>
                        <#return taskClass/>
                    <#case 'unmerged'>
                        <#assign class = taskClass/>
                </#switch>
            </#list>
        </#if>
    </#if>
    <#return class/>
</#function>

<#function defineProjectClass project>
    <#compress>
        <#if (project.failedTasksCount > 0)>
            <#return 'failed'/>
        <#elseif project.tasks?size == 0 || tasksNeverExecuted(project.tasks)>
            <#return 'empty '/>
        <#elseif isProjectMerged(project)>
            <#return 'merged'/>
        <#else>
            <#return 'unmerged'/>
        </#if>
    </#compress>
</#function>

<#function defineTaskClass task>
    <#compress>
        <#if task.stackTrace??>
            <#return 'failed'/>
        <#elseif !task.executed??>
            <#return 'empty'/>
        <#elseif (task.unmergedRevisions?size > 0)>
            <#return 'unmerged'/>
        <#else>
            <#return 'merged'/>
        </#if>
    </#compress>
</#function>

<#function isProjectMerged project>
    <#list project.tasks as task>
        <#if (task.unmergedRevisions?size > 0)>
            <#return false/>
        </#if>
    </#list>
    <#return true/>
</#function>

<#function tasksNeverExecuted tasks>
    <#list tasks as task>
        <#if task.executed??>
            <#return false/>
        </#if>
    </#list>
    <#return true/>
</#function>
